<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('productorder.quick Search Fields') })"
        >


            <el-button v-auth="'write'" v-blur class="table-header-operate mlr-12" style="margin-left: 12px"
                       type="success" @click="write_show = true">
                <Icon color="#ffffff" name="el-icon-Management"/>
                <span class="table-header-operate-text">核销</span>
            </el-button>

        </TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef"></Table>

        <!-- 表单 -->
        <PopupForm />


        <el-dialog
            v-model="write_show"
            title="核销"
            width="500"
        >

            <FormItem :label="t('productorder.write_code')" style="width: 90%;margin-left: 20px;" type="string" v-model="write_code" :placeholder="t('Please input field', { field: t('productorder.write_code') })"/>

            <FormItem :label="t('productorder.write_num')" style="width: 90%;margin-left: 20px;" type="number" v-model="write_num" :placeholder="t('Please input field', { field: t('productorder.write_num') })"/>

            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="write_show = false">取消</el-button>
                    <el-button type="primary" @click="sub_write">
                        核销
                    </el-button>
                </div>
            </template>
        </el-dialog>

        <Info/>
    </div>
</template>

<script setup lang="ts">
import { onMounted, provide, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import { defaultOptButtons } from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'
import createAxios from "/@/utils/axios";
import {ElMessage} from "element-plus";
import {auth} from "/@/utils/common";
import Info from './info.vue'
import FormItem from "/@/components/formItem/index.vue";

defineOptions({
    name: 'productorder',
})

const { t } = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])


const info_auth = auth('info')
const refund_auth = auth('refund')

optButtons.push(
    {
        render: 'tipButton',
        // name 是任意的
        name: 'info',
        // title 是语言翻译 key
        title: '详情',
        text: '',
        // type: 'warning',
        // icon: 'fa fa-search-plus icon',
        type: 'primary',
        icon: 'fa fa-info-circle',
        click(row, field) {
            // 获取详情
            createAxios({
                url: '/admin/productorder/info?id='+row.id,
                method: 'get',
                data: {
                    id: row.id,
                }
            }, {
                loading: true
            }, {
                text: '加载中...'
            })
                .then((res) => {
                    if (res.code == 1) {

                        // 在 extend 上自定义一个变量标记详情弹窗显示状态，详情组件内以此判断显示即可！

                        baTable.table.extend!.showInfo = true
                        baTable.table.extend!.infoData = res.data.row
                    } else {

                        ElMessage({
                            message: res.msg,
                            type: 'error',
                        })
                    }


                })
                .catch((resx) => {
                })
        },

        display: (row, field) => {
            if (info_auth){
                return true;
            }
        },
    },

    {
        render: 'confirmButton',
        name: 'refund_money',
        title: '退款',
        // text: '退款',
        type: 'info',
        icon: 'fa fa-sign-out',
        class: 'table-row-info',
        popconfirm: {
            title:'确认退款吗？',
        },

        click(row, field) {
            // 获取详情
            createAxios({
                url: '/admin/productorder/refund',
                method: 'post',
                data: {
                    id:row.id,
                }
            }, {
                loading: true
            }, {
                text: '退款中...'
            })
                .then((res) => {
                    ElMessage({
                        message: res.msg,
                        type: 'success',
                    })

                })
                .catch((resx) => {
                })
        },

        display: (row, field) => {
            if (refund_auth && row.refund_status == 3){
                return true;
            }
        },
    },
)
/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/Productorder/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            // { label: t('productorder.id'), prop: 'id', align: 'center', width: 70, operator: 'RANGE', sortable: 'custom' },
            { label: t('productorder.user__nickname'),width: 100, prop: 'user.nickname', align: 'center', operatorPlaceholder: t('Fuzzy query'),  operator: 'LIKE' },
            // { label: t('productorder.shop_id'), prop: 'shop_id', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE' },
            { label: t('productorder.shop__name'),width: 200, prop: 'shop.name', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE' },

            {
                label: t('productorder.market__name'),
                width: 100,
                prop: 'market.name',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE'
            },

            { label: t('productorder.name'),width: 150, prop: 'name', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            // { label: t('productorder.product__name'),width: 150, prop: 'product.name', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE' },
            { label: t('productorder.order_no'),width: 200, prop: 'order_no', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            // { label: t('productorder.pay_order'), prop: 'pay_order', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            //
            // { label: t('productorder.image'), prop: 'image', align: 'center', render: 'image', operator: false },
            { label: t('productorder.price'),width: 100, prop: 'price', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('productorder.num'),width: 100, prop: 'num', align: 'center', operator: 'RANGE', sortable: false },
            // { label: t('productorder.num'), prop: 'num', align: 'center', operator: 'RANGE', sortable: false },
            // { label: t('productorder.order_money'), prop: 'order_money', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('productorder.vip_price'),width: 100, prop: 'vip_price', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('productorder.pay_money'),width: 100, prop: 'pay_money', align: 'center', operator: 'RANGE', sortable: false },
            { label: t('productorder.pay_time'), prop: 'pay_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            // { label: t('productorder.pay_type'), prop: 'pay_type', align: 'center', operator: 'eq', sortable: false, replaceValue: { '1': t('productorder.pay_type 1'), '2': t('productorder.pay_type 2'), '3': t('productorder.pay_type 3'), '9': t('productorder.pay_type 9') } },
            // { label: t('productorder.pay_status'), prop: 'pay_status', align: 'center', render: 'tag', operator: 'eq', sortable: false, replaceValue: { '0': t('productorder.pay_status 0'), '1': t('productorder.pay_status 1') } },
            { label: t('productorder.order_status'),width: 100, prop: 'order_status', align: 'center', render: 'tag', operator: 'eq', sortable: false, replaceValue: { '1': t('productorder.order_status 1'), '10': t('productorder.order_status 10'), '40': t('productorder.order_status 40') , '50': t('productorder.order_status 50'), '90': t('productorder.order_status 90') } },

            { label: t('productorder.refund_status'),width:100, prop: 'refund_status', render: 'tag', align: 'center', operator: 'eq', sortable: false, replaceValue: { '0': t('productorder.refund_status 0'), '1': t('productorder.refund_status 1'), '2': t('productorder.refund_status 2'), '3': t('productorder.refund_status 3'), '4': t('productorder.refund_status 4') } },

            { label: t('productorder.use_time'), prop: 'use_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            { label: t('productorder.create_time'), prop: 'create_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            // { label: t('productorder.confirm_time'), prop: 'confirm_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            // { label: t('productorder.cancel_time'), prop: 'cancel_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            // { label: t('productorder.qrcode'), prop: 'qrcode', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            // { label: t('productorder.update_time'), prop: 'update_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            // { label: t('productorder.user_del_time'), prop: 'user_del_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            { label: t('Operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false, fixed: 'right' },
        ],
        dblClickNotEditColumn: false,
    },
    {
        defaultItems: { price: 0, num: 1, order_money: 0, vip_price: 0, pay_money: 0, pay_time: null, pay_type: '1', pay_status: '1', order_status: '1', use_time: null, confirm_time: null, cancel_time: null, user_del_time: null },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})



/**
 * 核销
 */
const write_show = ref(false);
const write_code = ref();
const write_num = ref(1);
const sub_write = () => {

    createAxios({
        url: '/admin/productorder/write',
        method: 'post',
        data: {
            write_code: write_code.value,
            write_num: write_num.value,
        }
    }, {
        loading: true
    }, {
        text: '核销中...'
    })
        .then((res) => {
            ElMessage({
                message: res.msg,
                type: 'success',
            })

            write_show.value = false;
            write_code.value = '';

            baTable.onTableHeaderAction('refresh', {})

        })
        .catch((resx) => {
        })
}

</script>

<style scoped lang="scss"></style>
